package taskadvanced.ui.handlers;

import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.ui.ISelectionListener;
import org.eclipse.ui.IViewPart;
import org.eclipse.ui.IViewReference;
import org.eclipse.ui.IWorkbenchPart;

import taskadvanced.Activator;

public class Upload extends AbstractHandler {
	private ISelectionListener mylistener = new ISelectionListener() {
        public void selectionChanged(IWorkbenchPart sourcepart, ISelection selection) {
      
            //System.out.println((((IStructuredSelection) selection).toList()));
        	System.out.println("selection changed");
        	//ITreeSelection ss = (ITreeSelection) selection;
			//System.out.println(ss.getFirstElement());

        }
    };
    
	@Override
	public Object execute(ExecutionEvent event) throws ExecutionException {
		IViewReference[] stg = Activator.getDefault().getWorkbench().getActiveWorkbenchWindow().getActivePage().getViewReferences();
		//Activator.getDefault().getWorkbench().getActiveWorkbenchWindow().getActivePage().getViewReferences()[1].get
		int tasksLocation = -1;
		for (int i=0;i<stg.length;i++){
			if (stg[i].getTitle().substring(0, 5).compareTo("Tasks") == 0)
				tasksLocation = i;
		}
		IViewPart something = stg[tasksLocation].getView(false);

		something.getSite().getWorkbenchWindow().getSelectionService().addSelectionListener(mylistener);

		return null;
	}
	
	

}
